package com.aptemo.webetek.client.widgets.stakes;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.utils.TokenUtils;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.BetBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class Stakes
		extends Composite implements StakesDisplay
{

	private static StakesUiBinder	uiBinder	= GWT.create(StakesUiBinder.class);

	interface StakesUiBinder extends UiBinder<Widget, Stakes>
	{
	}

	@UiField
	FlexTable						contentPanel;

	@UiField
	Label							infoField;

	@UiField
	Hyperlink						moreLink;

	@UiField
	PagingView						pagingPanel;

	@UiField
	ListBox							statusFilter;

	private StakesPagingPresenter	presenter;

	public Stakes()
	{
		initWidget(uiBinder.createAndBindUi(this));

		// init enum
		// statusFilter.addItem(Webetek.getConstants().all(), "");
		for (EnumBetStatus item : EnumBetStatus.values())
		{
			try
			{
				String trans = Webetek.getConstants().getString(item.name());
				statusFilter.addItem(trans, item.toString());
			}
			catch (MissingResourceException e)
			{
				statusFilter.addItem(item.toString(), item.toString());
			}
		}

	}

	@Override
	public void updateStakeList(List<BetBean> list)
	{
		contentPanel.removeAllRows();

		if (list == null || list.size() == 0)
		{
			moreLink.setVisible(false);
			pagingPanel.setVisible(false);
			contentPanel.setText(0, 0, Webetek.getConstants().noEntries());
			return;
		}

		// check if widget is on main window
		String tmp = TokenUtils.getTokenPart(History.getToken(), "stake");
		if (!tmp.isEmpty())
		{
			pagingPanel.setVisible(true);
			moreLink.setVisible(false);
		}
		else
		{
			moreLink.setVisible(true);
		}

		int index = 0;
		for (BetBean item : list)
		{
			// contentPanel.add(item);
			StakeRow row = new StakeRow(item);
			StakeRowPresenter pr = new StakeRowPresenter(item, null, row, null);
			pr.go(null);
			contentPanel.setWidget(index++, 0, row);
		}
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasClickHandlers getList()
	{
		return contentPanel;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{
		int selectedRow = -1;
		HTMLTable.Cell cell = contentPanel.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			//
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public PagingDisplay getPagingView()
	{
		return pagingPanel;
	}

	@Override
	public HasChangeHandlers getStatusFilter()
	{
		return statusFilter;
	}

	@Override
	public EnumBetStatus getStatusFilterValue()
	{
		int i = statusFilter.getSelectedIndex();

		String s = statusFilter.getValue(i);
		if (s.isEmpty())
		{
			return null;
		}

		return EnumBetStatus.valueOf(s);
	}

	@Override
	public void setControlsMode(boolean visible)
	{
		pagingPanel.setVisible(visible);
		// reinvestField.setVisible(visible);
		// reinvestBtn.setVisible(visible);
		moreLink.setVisible(!visible);
		statusFilter.setEnabled(visible);
	}

	@Override
	public void setTarget(String target)
	{
		moreLink.setTargetHistoryToken("stake-" + target);
	}

	@Override
	public void setStatusValue(EnumBetStatus status)
	{
		for (int i = 0; i < statusFilter.getItemCount(); i++)
		{
			String val = statusFilter.getValue(i);

			if (!val.isEmpty() && EnumBetStatus.valueOf(val) != null && EnumBetStatus.valueOf(val).equals(status))
			{
				statusFilter.setSelectedIndex(i);

				String token = History.getToken();
				if (token.contains("stake") && !TokenUtils.getTokenPart(token, "stake").isEmpty())
				{
					token = TokenUtils.setToken(token, "filter", val);
					History.newItem(token, false);
				}
				return;
			}
		}

	}

	@UiHandler("statusFilter")
	void onStatusChanged(ChangeEvent event)
	{
		String token = History.getToken();
		// check if main
		String tmp = TokenUtils.getTokenPart(token, "stake");
		if (tmp.isEmpty())
		{
			// do it internally
			// presenter.update(currentUser, teamId, filter, pageNo)
			return;
		}

		if (getStatusFilterValue() != null)
		{
			token = TokenUtils.setToken(token, "filter", getStatusFilterValue().toString());
		}
		else
		{
			token = TokenUtils.setToken(token, "filter", "");
			// token = TokenUtils.removeToken(token, "filter");
		}
		History.newItem(token, false);
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
